//
//  UserLoginRegistrationBaseViewController.swift
//  SitPosition
//
//  Created by apple on 2018/12/31.
//  Copyright © 2018 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography


let kMaxPhonePasswordLength: Int = 16  // 手机号、密码最大长度

class UserLoginRegistrationBaseViewController : GKNavigationBarViewController {
    
    // 返回事件
    var backCallback: VoidCallback = { }
    
    // 背景图
    let backgroundView: UserLoginRegistrationBackground = UserLoginRegistrationBackground()
    
    // 底部占位IV
    lazy var bodyIV: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "logon_body"))
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = true
        return iv
    }()
    
    // 标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.Color.primaryLightTextColor
        label.font = Theme.Font.boldFont(ofSize: 26)
        label.numberOfLines = 1
        label.lineBreakMode = .byTruncatingTail
        return label
    }()
    
    // 获取验证码按钮
    lazy var getVerifyCodeButton: UIButton = {
        let btn = UIButton(type: UIButton.ButtonType.custom)
        btn.layer.borderWidth = 1
        btn.layer.cornerRadius = 2
        btn.layer.masksToBounds = true
        btn.layer.borderColor = Theme.Color.primaryBlueColor.cgColor
        btn.titleLabel?.font = Theme.Font.regularFont(ofSize: 12)
        btn.setTitleColor(Theme.Color.primaryLightTextColor, for: UIControl.State.normal)
        btn.setTitle(Strings.logonGetVerifyCode, for: UIControl.State.normal)
        return btn
    }()
    
    // MARK: - Object lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 导航栏
        gk_navBackgroundColor = UIColor.clear
        
        // 点击view收起键盘
        let gr = UITapGestureRecognizer(target: self, action: #selector(_dismissKeyboard))
        view.addGestureRecognizer(gr)
        
        // 背景图
        view.insertSubview(backgroundView, at: 0)
        constrain(backgroundView, view) { backgroundView, view in
            backgroundView.edges == view.edges
        }
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return UIStatusBarStyle.lightContent
    }
    
    // 收起键盘
    @objc func _dismissKeyboard() {
        view.endEditing(true)
    }
    
    // 构建UITextField
    // @placeholder：占位字符串
    // @leftView：左边修饰视图
    func _buildTextField(placeholder: String? = nil, leftView: UIView? = nil) -> TextFieldWithLRView {
        let input = TextFieldWithLRView()
        input.tintColor = Theme.Color.primaryLightTextColor
        input.textColor = Theme.Color.primaryLightTextColor
        if let placeholder_ = placeholder {
            input.attributedPlaceholder = {
                let attributes = [NSAttributedString.Key.foregroundColor : Theme.Color.primaryBlueColor]
                let string = NSAttributedString(string: placeholder_, attributes: attributes)
                return string
            }()
        }
        input.font = Theme.Font.regularFont(ofSize: 14)
        input.leftView = leftView
        input.leftViewMode = .always
        return input
    }
}
